<?php

include("../config/db_config.php");
include("../vendor/php-pdo/class.db.php");
include("../vendor/simplehtmldom/simple_html_dom.php");
include("../vendor/amazon-ecs/lib/AmazonECS.class.php");

$accord_db = new db("mysql:host=127.0.0.1;port=3306;dbname=accord", "root", "");

$categories = $accord_db->select("categories", "active = 1");
$models = $accord_db->select("models");

foreach ($categories as $cat) {
    foreach ($models as $mod) {
        echo "\n*** " . $mod['year'] . ' ' . $mod['att1'] . ' ' . $mod['att2'] . ' ' . $mod['att3'] . ' [' . $cat['name'] . "] ***\n";
        $url = "http://www.amazon.com/s/?" . $cat['path'] . '&vehicle=' . $mod['path'];
        $html = file_get_html($url);
        //echo $html->outertext."\n";
        $total_page = !is_null($html->find('span.pagnDisabled', 0)) ? $html->find('span.pagnDisabled', 0)->plaintext : 1;
        echo "*** total pages: " . $total_page . "\n";
        echo "*** url: " . $url . "\n";
        echo "*** asin: ";
        foreach ($html->find('li.s-result-item') as $element) {
            $asin = $element->{'data-asin'};
            $exits_product = $accord_db->select("products", "asin='" . $asin . "'");
            if (!$exits_product) {
                $product_insert = array("asin" => $asin, "created_time"=>time());
                $accord_db->insert("products", $product_insert);
                echo $asin, " [new] ";
            }
            $bind = array(":product_asin" => $asin, ":cat_id" => $cat['id'], ":model_id" => $mod['id']);
            $exits_product_cat_model_lkp = $accord_db->select("product_cat_model_lkp", "product_asin = :product_asin AND cat_id = :cat_id AND model_id = :model_id", $bind);
            if (!$exits_product_cat_model_lkp) {
                $product_cat_model_lkp_insert = array("product_asin" => $asin, "cat_id" => $cat['id'], "model_id" => $mod['id']);
                $accord_db->insert("product_cat_model_lkp", $product_cat_model_lkp_insert);
            }
        }
        echo "\n";
        if ($total_page >= 2) {
            for ($i = 2; $i <= $total_page; $i++) {
                $next_url = "http://www.amazon.com/s/?" . $cat['path'] . '&vehicle=' . $mod['path'] . "&page=" . $i;
                $next_html = file_get_html($next_url);
                echo "*** url: " . $next_url . "\n";
                echo "*** asin: ";
                foreach ($next_html->find('li.s-result-item') as $element) {
                    $asin = $element->{'data-asin'};
                    $exits_product = $accord_db->select("products", "asin='" . $asin . "'");
                    if (!$exits_product) {
                        $product_insert = array("asin" => $asin, "created_time"=>time());
                        $accord_db->insert("products", $product_insert);
                        echo $asin," [new] ";
                    }
                    $bind = array(":product_asin" => $asin, ":cat_id" => $cat['id'], ":model_id" => $mod['id']);
                    $exits_product_cat_model_lkp = $accord_db->select("product_cat_model_lkp", "product_asin = :product_asin AND cat_id = :cat_id AND model_id = :model_id", $bind);
                    if (!$exits_product_cat_model_lkp) {
                        $product_cat_model_lkp_insert = array("product_asin" => $asin, "cat_id" => $cat['id'], "model_id" => $mod['id']);
                        $accord_db->insert("product_cat_model_lkp", $product_cat_model_lkp_insert);
                    }
                }
                echo "\n";
            }
        }
    }
}